package com.dudsbury.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import com.dudsbury.database.DatabaseExtension;
import com.dudsbury.database.MyItem;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class CreateItemScreen extends JFrame {

	private JPanel contentPane;
	private JTextField nameText;
	private JTextField priceText;
	private JTextArea descText;
	private JComboBox menuTypeCombo;
	private String id;
	/*
	Standard Meal - Vegetarian 
	Standard Meal - Non-Vegetarian
	Alcoholic Drink
	Non-Alcoholic Drink
	Other Item
	Buffet A - Non-Vegetarian
	Buffet B - Non-Vegetarian
	Buffet A - Vegetarian
	Buffet B - Vegetarianet
	*/
	
	public static final int STANDARD_MEAL_VEG = 0;
	public static final int STANDARD_MEAL = 1;
	public static final int ALCOHOLIC_DRINK = 2;
	public static final int NON_ALCOHOLIC = 3;
	public static final int OTHER_ITEM = 4;
	public static final int BUFFET_A_NON_VEG = 5;
	public static final int BUFFET_B_NON_VEG = 6;
	public static final int BUFFET_A_VEG = 7;
	public static final int BUFFET_B_VEG = 8;
	MyItem item;
	
	JFrame thisFrame = this;
	SearchScreen screen;
	public CreateItemScreen(MyItem item,SearchScreen screen) {
		this();
		this.item=item;
		this.screen=screen;
		//nameText = new JTextField();
		//descText = new JTextArea();
		//priceText = new JTextField();
		id = item.getItemID();
		nameText.setText(item.getItemName());
		descText.setText(item.getItemDescription());
		menuTypeCombo.setSelectedIndex(item.getItemType());
		priceText.setText("" + item.getPrice());
		
		
	}
	public CreateItemScreen() {
		setTitle("New Item");
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 648, 357);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			String[] data = new String[5];
			try {
			data[0] = item.getItemID();
			}catch(Exception e) {
				System.out.println("No Item ID Found");
				data[0]="";
			}
			
			
			if (nameText.getText().trim().length()<1) {
				JOptionPane.showMessageDialog(null, "Name may not be empty"); 
				return;
			}
			else {
				try {
					float f = Float.valueOf(priceText.getText());
				}catch (Exception e) {
					JOptionPane.showMessageDialog(null, "Price must be a number");
					return;
				}
			}
			data[1] = nameText.getText();
			data[2] = descText.getText();
			data[3] = "" + menuTypeCombo.getSelectedIndex();
			data[4] = priceText.getText();
			MyItem item = new MyItem(data);
			
			
			DatabaseExtension.saveItem(item);
			if (screen!=null)
			screen.search();
			thisFrame.dispose();
			}
		});
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nameText.setText("");
				priceText.setText("");
				descText.setText("");
				menuTypeCombo.setSelectedIndex(0);
			}
		});
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				thisFrame.dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(btnSave)
									.addGap(41)
									.addComponent(btnClear)
									.addGap(40)
									.addComponent(btnExit)
									.addGap(36))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 309, GroupLayout.PREFERRED_SIZE)
									.addContainerGap())))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
							.addGap(11)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnExit)
								.addComponent(btnClear)
								.addComponent(btnSave))))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JLabel lblMealType = new JLabel("Item Type:");
		
		menuTypeCombo = new JComboBox();
		menuTypeCombo.setModel(new DefaultComboBoxModel(new String[] {"Standard Meal - Vegetarian ", "Standard Meal - Non-Vegetarian", "Alcoholic Drink", "Non-Alcoholic Drink", "Other Item", "Buffet A - Non-Vegetarian", "Buffet B - Non-Vegetarian", "Buffet A - Vegetarian", "Buffet B - Vegetarian"}));
		
		JLabel lblPrice = new JLabel("Price:");
		
		priceText = new JTextField();
		priceText.setColumns(10);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(lblMealType)
						.addComponent(lblPrice))
					.addGap(18)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(priceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(menuTypeCombo, 0, 177, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMealType)
						.addComponent(menuTypeCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPrice)
						.addComponent(priceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(28, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JLabel lblMealName = new JLabel("Item Name:");
		
		nameText = new JTextField();
		nameText.setColumns(10);
		
		JLabel lblMealDescription = new JLabel("Item Description:");
		lblMealDescription.setHorizontalAlignment(SwingConstants.CENTER);
		
		 descText = new JTextArea();
		 descText.setLineWrap(true);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(descText, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblMealName)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(nameText, GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
						.addComponent(lblMealDescription, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMealName)
						.addComponent(nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(lblMealDescription)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(descText, GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel lblCreateMeal = new JLabel("Enter Item Details");
		lblCreateMeal.setHorizontalAlignment(SwingConstants.CENTER);
		lblCreateMeal.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCreateMeal, GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCreateMeal, GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}

}
